package com.boy.rpc.framework.console.utils;

import com.boy.rpc.framework.console.Constant;
import com.boy.rpc.framework.console.bean.Pagination;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by wesley on 2017-03-16.
 */
public class PaginationUtils {

    public static Pagination stringListPagination(HttpServletRequest request, String queryKey, String queryKey_ch, String queryValue, String queryByKeyPrefixs) {
        Pagination pagination = getPagination(request);
        String url = "?" + "queryKey=" + queryKey + "&queryKey_ch=" + queryKey_ch + "&queryValue=" + queryValue;
        pagination.setLink_to(url);
        if(!StringUtil.isEmpty(queryByKeyPrefixs)) {

        }
        return pagination;
    }

    public static Pagination getPagination(HttpServletRequest request) {
        String items_per_page = StringUtil.getParameterByDefault(request, "items_per_page", Constant.DEFAULT_ITEMS_PER_PAGE +"");
        String num_display_entries = StringUtil.getParameterByDefault(request, "num_display_entries", "3");
        String visit_page = StringUtil.getParameterByDefault(request, "visit_page", "0");
        String num_edge_entries = StringUtil.getParameterByDefault(request, "num_edge_entries", "2");
        String prev_text = StringUtil.getParameterByDefault(request, "prev_text", "Prev");
        String next_text = StringUtil.getParameterByDefault(request, "next_text", "Next");
        String ellipse_text = StringUtil.getParameterByDefault(request, "ellipse_text", "Next");
        String prev_show_always = StringUtil.getParameterByDefault(request, "prev_show_always", "true");
        String next_show_always = StringUtil.getParameterByDefault(request, "next_show_always", "true");

        Pagination pagination = new Pagination();
        pagination.setItems_per_page(Integer.parseInt(items_per_page));
        pagination.setNum_display_entries(Integer.parseInt(num_display_entries));
        pagination.setCurrent_page(Integer.parseInt(visit_page));
        pagination.setNum_edge_entries(Integer.parseInt(num_edge_entries));
        pagination.setPrev_text(prev_text);
        pagination.setNext_text(next_text);
        pagination.setEllipse_text(ellipse_text);
        pagination.setPrev_show_always(Boolean.parseBoolean(prev_show_always));
        pagination.setNext_show_always(Boolean.parseBoolean(next_show_always));

        return pagination;
    }
}
